using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using AttributeRouting.Web.Mvc;
using LastoRevival.Service.Contracts.Models;

namespace LastoRevival.Website.Controllers
{
	public class AdminController : LastoControllerBase
    {

		[GET("admin/environment")]
        public ViewResult Environment()
        {
			// show env variables
            return View();
        }

		[GET("admin/forum/create")]
		public ViewResult CreateForum()
		{
			var model = new CreateForumModel();
			return View(model);
		}

		[POST("admin/forum/create")]
		public ActionResult CreateForum(CreateForumModel model)
		{
			if (ModelState.IsValid)
			{
				var forumID = Service.CreateForum(LoginState.Token, model);

				return RedirectToAction("EditForumAccessForm", "Admin", new { forumID = forumID });
			}
			
			// failed due to some validation error
			return View(model);
		}

		[GET("admin/forum/{forumId}/edit/access")]
		public ViewResult EditForumAccessForm(int forumID)
		{
			var permissions = Service.GetForumAccessPermissions(LoginState.Token, forumID);
			var forumName = Service.GetForum(LoginState.Token, forumID).Title;

			ViewBag.ForumTitle = forumName;

			return View("EditForumAccess", permissions);
		}

		[POST("admin/forum/edit/access")]
		public JsonResult EditForumAccess(UpdateGroupPermissionsModel model)
		{
			if (!ModelState.IsValid)
			{
				return Json(false);
			}

			Service.UpdateForumAccessPermissions(LoginState.Token, model);

			return Json(true);
		}

		[GET("admin/forum/delete")]
		public ViewResult ConfirmDeleteForum(int forumID)
		{
			var forum = Service.GetForum(LoginState.Token, forumID);

			ViewBag.ForumTitle = forum.Title;
			ViewBag.ForumID = forumID;

			return View();
		}

		[POST("admin/forum/delete")]
		public ActionResult DeleteForum(int forumID)
		{
			Service.DeleteForum(LoginState.Token, forumID);

			return RedirectToAction("Index", "Forum");
		}

		[GET("admin/forum/{forumId}/edit")]
		public ViewResult EditForumForm(int forumID)
		{
			var forum = Service.GetForumForEdit(LoginState.Token, forumID);

			ViewBag.ForumTitle = forum.Title;

			return View("EditForum", forum);
		}

		[POST("admin/forum/edit")]
		public ActionResult EditForum(EditForumModel model)
		{

			// special handing for missing forum ID
			if (model.ForumID == 0)
			{
				throw new Exception("Forum ID is missing");
			}

			if (ModelState.IsValid)
			{
				Service.UpdateForum(LoginState.Token, model);

				return RedirectToAction("ViewForum", "Forum", new { forumID = model.ForumID });
			}

			// failed, return
			var forum = Service.GetForumForEdit(LoginState.Token, model.ForumID);

			ViewBag.ForumTitle = forum.Title;

			return View(model);
		}

		[GET("admin/lock/{postId}")]
		public ActionResult LockPost(int postId)
		{
			var post = Service.GetPostForViewing(LoginState.Token, postId);

			Service.ChangeLockState(LoginState.Token, postId, true);

			return RedirectToAction("ViewForum", "Forum", new { forumId = post.ForumID });
		}

		[GET("admin/unlock/{postId}")]
		public ActionResult UnlockPost(int postId)
		{
			var post = Service.GetPostForViewing(LoginState.Token, postId);

			Service.ChangeLockState(LoginState.Token, postId, false);

			return RedirectToAction("ViewForum", "Forum", new { forumId = post.ForumID });
		}

    }
}
